package cn.stylefeng.guns.modular.utils;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONUtil;
import com.jushuitan.api.ApiClient;
import com.jushuitan.api.ApiRequest;
import com.jushuitan.api.ApiResponse;
import com.jushuitan.api.DefaultApiClient;
import com.jushuitan.api.exception.ApiException;

import java.util.HashMap;
import java.util.Map;

/**
 * 调用Jushuitan API 出库订单
 * @author Administrator
 */
public class JstDdcx {


    public static ApiResponse  getJstDdcx(String appKey,String appSecret,String accessToken,
                                          String o_ids,String modifiedBegin,String modifiedEnd) {
        //https://openapi.jushuitan.com/open/webapi/wmsapi/purchasein/purchaseinquery
        ApiClient client = new DefaultApiClient();
        // 设置超时时间
        //        ((DefaultApiClient)client).setReadTimeout(3000);
        //        ((DefaultApiClient)client).setConnectTimeout(2000);

        // 替换为POP分配给应用的app_key,app_secret,accessToken等
        String url = "https://openapi.jushuitan.com/open/orders/out/simple/query";// 示例为测试地址，生产上请替换为正式地址
//        String appKey = "c8e0d3e0782941e38f8da4d2f5a192f3";
//        String appSecret = "99f4816e1dc543c78acefb79c8c66337";
//        String accessToken = "1e321502867f4e09bda116aabd765a50";
        Map<String,Object> bizMap = new HashMap<>();
        bizMap.put("modified_begin",modifiedBegin);
        bizMap.put("modified_end",modifiedEnd);
//        bizMap.put("o_ids", ListUtil.of("551127"));

        //String biz = "{\"shop_id\":10187992,\"sku_ids\":\"lyyi001,lyyi002\",\"page_index\":1,\"page_size\":30}";
        // 构建请求对象
        ApiRequest request = new ApiRequest.Builder(url, appKey, appSecret)
                .biz(JSONUtil.toJsonStr(bizMap)).build();
        ApiResponse response = null;
        // 执行接口调用
        try {
            response = client.execute(request, accessToken);
            System.out.println("is success: " + response.isSuccess());
            System.out.println("body: " + response.getBody());
        } catch (ApiException e) {
            switch (e.getCodeEnum()) {
                case SYS_ERROR:
                    // TODO retry
                    break;
                case PARAM_ERROR:
                    // TODO check param
                    break;
                case SIGN_ERROR:
                    // TODO @see com.jushuitan.api.util.ApiUtil
                    break;
                case CONNECT_ERROR:
                    // TODO check network
                    break;
                case TIME_OUT_ERROR:
                    // TODO check logic or reset readTimeout
                    break;
                case EXECUTE_ERROR:
                    // TODO @see com.jushuitan.api.DefaultApiClient#executeRemote
                    break;
                default:
                    // log
                    break;
            }
        } catch (Exception e) {
            // exception handle
        }

        return response;
    }

    public static void main(String[] args) {
        //https://openapi.jushuitan.com/open/webapi/wmsapi/purchasein/purchaseinquery
        ApiClient client = new DefaultApiClient();
        // 设置超时时间
        //        ((DefaultApiClient)client).setReadTimeout(3000);
        //        ((DefaultApiClient)client).setConnectTimeout(2000);

        // 替换为POP分配给应用的app_key,app_secret,accessToken等
        String url = "https://openapi.jushuitan.com/open/orders/out/simple/query";// 示例为测试地址，生产上请替换为正式地址
        String appKey = "c8e0d3e0782941e38f8da4d2f5a192f3";
        String appSecret = "99f4816e1dc543c78acefb79c8c66337";
        String accessToken = "1e321502867f4e09bda116aabd765a50";
        Map<String,Object> bizMap = new HashMap<>();
//        bizMap.put("modified_begin","2025-05-14");
//        bizMap.put("modified_end","2025-05-15");
        bizMap.put("o_ids", ListUtil.of("569690"));

        //String biz = "{\"shop_id\":10187992,\"sku_ids\":\"lyyi001,lyyi002\",\"page_index\":1,\"page_size\":30}";
        // 构建请求对象
        ApiRequest request = new ApiRequest.Builder(url, appKey, appSecret)
                .biz(JSONUtil.toJsonStr(bizMap)).build();

        // 执行接口调用
        try {
            ApiResponse response = client.execute(request, accessToken);
            System.out.println("is success: " + response.isSuccess());
            System.out.println("body: " + response.getBody());
        } catch (ApiException e) {
            switch (e.getCodeEnum()) {
                case SYS_ERROR:
                    // TODO retry
                    break;
                case PARAM_ERROR:
                    // TODO check param
                    break;
                case SIGN_ERROR:
                    // TODO @see com.jushuitan.api.util.ApiUtil
                    break;
                case CONNECT_ERROR:
                    // TODO check network
                    break;
                case TIME_OUT_ERROR:
                    // TODO check logic or reset readTimeout
                    break;
                case EXECUTE_ERROR:
                    // TODO @see com.jushuitan.api.DefaultApiClient#executeRemote
                    break;
                default:
                    // log
                    break;
            }
        } catch (Exception e) {
            // exception handle
        }

    }
}
